<template>


  <div>

    <el-upload
        class="avatar-uploader"
        action="http://localhost:9066/upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
    >
      <img v-if="avatarUrl" :src="avatarUrl" class="avatar" />



      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-form ref="form" :model="form" label-width="80px">

      <el-form-item label="用户名">
        {{showusername}}
        
      </el-form-item>

      <el-form-item label="出生时间">
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择日期" v-model="form.date1" style="width: 100%;"></el-date-picker>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-time-picker type="fixed-time" placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>
        </el-col>
      </el-form-item>
    </el-form>
<!--    <el-button @click="saveAvatar">获取图片请求</el-button>-->

    <!-- 图片回显示  比如它回显在顶部头像，我放在这里是方便演示-->
<!--    <div class="avatar">-->
<!--      <img width="100%" height="100%" :src="avatarUrl" alt />-->
<!--    </div>-->
  </div>
</template>

<script>
import Cookie from 'js-cookie';
export default {
  data() {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },

      avatarUrl: "http://localhost:9066/jkl.jpg" //默认头像
    };
  },
  methods: {
    onSubmit() {
      console.log('submit!');
    },
    // 上传成功的函数
    handleAvatarSuccess(res, file) {
      // 上传成功 回显图片
      this.avatarUrl = URL.createObjectURL(file.raw);
      console.log(URL.createObjectURL(file.raw));
    },

    // 上传之前的限制函数
    beforeAvatarUpload(file) {
      // 类型
      const isJPG = file.type === "image/jpeg";
      // 大小
      const isLt2M = file.size / 1024 / 1024 < 2;
      // 类型限制
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      // 大小限制
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 回去头像的请求
    getAvatar() {
      this.req
          .get("upload")
          .then(response => {
            let data = response.data;
            this.avatarUrl =
                "http://localhost:9066" + data[data.length - 1].imgUrl;
            // console.log(data[data.length - 1].imgUrl);
          })
          .catch(err => {
            console.log(err);
          });
    }
  },
  created() {
    // 获取头像
    this.getAvatar();
  },
  computed:{
    showusername() {
      return  Cookie.get("username");
    }
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
